package org.example.partner.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import org.example.partner.domain.Partner;
import org.example.partner.service.IPartnerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合作伙伴Controller
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-04-11
 */
@Controller
@RequestMapping("/partner/partner")
public class PartnerController extends BaseController
{
    private String prefix = "partner/partner";

    @Autowired
    private IPartnerService partnerService;

    @GetMapping()
    public String partner(String partnerType, ModelMap mmap)
    {
        mmap.put("partnerType", partnerType);
        return prefix + "/partner";
    }

    @GetMapping("/choosePartner")
    public String choosePartner(String partnerType, ModelMap mmap)
    {
        mmap.put("partnerType", partnerType);
        return prefix + "/choosePartner";
    }


    /**
     * 查询合作伙伴列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Partner partner)
    {
        startPage();
        List<Partner> list = partnerService.selectPartnerList(partner);
        return getDataTable(list);
    }
    @RequiresPermissions("partner:partner:list")
    @PostMapping("/queryPartnerList")
    @ResponseBody
    public AjaxResult queryPartnerList(Partner partner)
    {
        List<Partner> list = partnerService.queryPartnerList(partner);
        if(list.size()>0){
            partner=list.get(0);
        }
        return AjaxResult.success(partner);
    }

    /**
     * 导出合作伙伴列表
     */
    @RequiresPermissions("partner:partner:export")
    @Log(title = "合作伙伴", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Partner partner)
    {
        List<Partner> list = partnerService.selectPartnerList(partner);
        ExcelUtil<Partner> util = new ExcelUtil<Partner>(Partner.class);
        return util.exportExcel(list, "合作伙伴数据");
    }

    /**
     * 新增合作伙伴
     */
    @GetMapping("/add")
    public String add(String partnerType,ModelMap mmap)
    {
        mmap.put("partnerType", partnerType);
        return prefix + "/add";
    }

    /**
     * 新增保存合作伙伴
     */
    @RequiresPermissions("partner:partner:add")
    @Log(title = "合作伙伴", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Partner partner)
    {
        return toAjax(partnerService.insertPartner(partner));
    }

    /**
     * 修改合作伙伴
     */
    @RequiresPermissions("partner:partner:edit")
    @GetMapping("/edit/{partnerId}")
    public String edit(@PathVariable("partnerId") Long partnerId, ModelMap mmap)
    {
        Partner partner = partnerService.selectPartnerByPartnerId(partnerId);
        mmap.put("partner", partner);
        return prefix + "/edit";
    }

    /**
     * 修改保存合作伙伴
     */
    @RequiresPermissions("partner:partner:edit")
    @Log(title = "合作伙伴", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Partner partner)
    {
        return toAjax(partnerService.updatePartner(partner));
    }

    /**
     * 删除合作伙伴
     */
    @RequiresPermissions("partner:partner:remove")
    @Log(title = "合作伙伴", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(partnerService.deletePartnerByPartnerIds(ids));
    }
}
